// pages/classify/classify.js
var utils = require('../../../utils/util.js');
import {
	Cart
} from '../../template/specMaskShopp/specMaskShopp.js';
var cart = new Cart();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        currentTab: 0,
        number: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        that.systemInfo();
        const res = wx.getSystemInfoSync()
        that.setData({
            height: res.windowHeight - 50,
           
        })
        console.log(that.data.number)
        that.categorylist();
        that.lll();

    },
	//我的个人信息
	personalInfo: function (e) {
		var that = this;
		utils.http("/api/v1/member/index", {}, function (res) {
			that.setData({
				info: res.datas,
			})

		})

	},
    onShow: function() {
        this.systemInfo();
		this.personalInfo();
        this.setData({
            number: wx.getStorageSync("wholesaleNum"),
        });

        this.lll();

    },
    lll: function(e) {
        var that = this;
        if (that.data.categoryData != undefined) {
            var categoryData = that.data.categoryData;
            for (var j in categoryData) {
                for (var i in categoryData[j].goods_list) {
                    categoryData[j].goods_list[i].number = 0;
                    for (var k in wx.getStorageSync("wholesaleCar")) {
                        if (wx.getStorageSync("wholesaleCar")[k].id == categoryData[j].goods_list[i].id) {
                            categoryData[j].goods_list[i].number = wx.getStorageSync("wholesaleCar")[k].number;
                        }
                    }
                }
            }
            that.setData({
                categoryData: categoryData
            })
        }
    },
    //分类商品切换接口
    categorylist: function(e) {
        var that = this;
        utils.http("/api/v1/category/category_list", {}, function(res) {
            that.setData({
                navData: res.datas
            })
            that.categoryGoodsList(res.datas[0].id);
        })
    },
    //分类获取商品列表接口
    categoryGoodsList: function(categoryId) {
        var that = this;
        utils.showLoading();
        utils.http("/api/v1/goods/category_goods_list", {
            category_id: categoryId
        }, function(res) {
            utils.hideLoading();
            for (var j in res.datas) {
                for (var i in res.datas[j].goods_list) {
                    res.datas[j].goods_list[i].number = 0;
                }
            }
            that.setData({
                categoryData: res.datas,
            })
            that.lll();
        })
    },
	//商品详情
	categorylistTap: function (id) {
		var that = this;
		utils.showLoading();
		utils.http("/api/v1/goods/detail", {
			id: id
		}, function (res) {
			utils.hideLoading();
			that.setData({
				dataDetail: res.datas,
			});
			cart.init(that, res.datas,function(){
				cart.showMask();
			});
			that.lll();
		})

	},
    // //商品详情
    // noFrutisDetailsTap: function(e) {
    //     var id = e.currentTarget.dataset.id;
    //     wx.navigateTo({
    //         url: '../commoditydetails/commoditydetails?id=' + id,
    //     })
    // },
    //tab 切换
    swichNav: function(e) {
        var cur = e.currentTarget.dataset.index;
        var id = e.currentTarget.dataset.id;
        if (this.data.currentTaB == cur) {
            return false;
        } else {
            this.setData({
                currentTab: cur
            })
        }
        this.categoryGoodsList(id);
    },
    //基本数据
    systemInfo: function(e) {
        var that = this;
        utils.showLoading();
        utils.http("/api/v1/common/system", {}, function(res) {
            utils.hideLoading();
            that.setData({
                system: res.datas
            })
            that.allNumber();
        })
    },
    //封装全局数量的加减
    allNumber: function(e) {
        var that = this;
        var price = 0;
        var totalPrice = 0;
        var itemslist = wx.getStorageSync("wholesaleCar");
        for (var i in itemslist) {
            price = itemslist[i].price
            if (itemslist[i].selectStatus == true) {
                totalPrice = totalPrice + (parseInt(itemslist[i].number) * parseFloat(price));
            }
        }
        that.setData({
            totalPrice: totalPrice.toFixed(2)
        })
        if (totalPrice > that.data.system.start_price) {
            that.setData({
                istips: true
            })
        } else {
            var chaNum = that.data.system.start_price - totalPrice;
            that.setData({
                istips: false,
                chaNum: chaNum
            })
        }
    },
    //减数量
    onNextTap: function(e) {
        var that = this;
        var itemslist = that.data.categoryData;
        var index = e.currentTarget.dataset.index;
        var ind = e.currentTarget.dataset.ind;
        if (itemslist[ind]['goods_list'][index].number > 0) {
            itemslist[ind]['goods_list'][index].number = itemslist[ind]['goods_list'][index].number - 1;
            var nowStorage = wx.getStorageSync("wholesaleCar");
            for (var i in nowStorage) {
                if (nowStorage[i].id == itemslist[ind]['goods_list'][index].id) {
                    nowStorage[i].number = nowStorage[i].number - 1;
                    if (nowStorage[i].number == 0) {
                        nowStorage.splice(i, 1);
                    }
                }
            }
            that.setData({
                number: wx.getStorageSync("wholesaleNum") - 1,
                categoryData: itemslist
            })
            wx.setStorageSync("wholesaleCar", nowStorage)
            wx.setStorageSync("wholesaleNum", that.data.number);
            // //微信小程序 tabbar 上面设置消息数目等
            if (wx.getStorageSync("wholesaleNum") != 0) {
                wx.setTabBarBadge({
                    index: 3,
                    text: String(wx.getStorageSync("wholesaleNum"))
                })
            } else {
                wx.removeTabBarBadge({
                    index: 3
                })
            }
        } else {
            utils.message("该宝贝宝贝不能减少哦~")
        }

        that.allNumber();
    },
    //加数量
    onPrevTap: function(e) {
        var that = this;
        var itemslist = that.data.categoryData;
        var index = e.currentTarget.dataset.index;
        var ind = e.currentTarget.dataset.ind;
        var num = e.currentTarget.dataset.number;
        var shuzi = wx.getStorageSync("wholesaleNum") || 0;
        var number = shuzi + 1
        that.setData({
            number: number
        })
        wx.setStorageSync("wholesaleNum", number);
        //微信小程序 tabbar 上面设置消息数目等
        if (wx.getStorageSync("wholesaleNum") != 0) {
            wx.setTabBarBadge({
                index: 3,
                text: String(wx.getStorageSync("wholesaleNum"))
            })
        }
        var dataInfo = {
            id: e.currentTarget.dataset.id,
            image: e.currentTarget.dataset.image,
            name: e.currentTarget.dataset.name,
            price: e.currentTarget.dataset.price,
            subtitle: e.currentTarget.dataset.subtitle,
            wholesalespec:e.currentTarget.dataset.wholesalespec,
            selectStatus: true, //购物车做删除时候用
            number: 1 //加减数量
        }
        itemslist[ind]['goods_list'][index].number = itemslist[ind]['goods_list'][index].number + 1;
        utils.uniqueSearchRecord1(dataInfo);
        that.setData({
            categoryData: itemslist
        })
        that.allNumber();
    },
    //进入购物车
    onCartButtonTap: function(e) {
        wx.navigateTo({
            url: '../../shcartshopp/shcartshopp',
        })
    },
    //搜索
    onSearchTap: function (e) {
        wx.navigateTo({
            url: '../../shsearch/shsearch',
        })
    },
	showMask: function (e) {
		console.log(e)
		this.categorylistTap(e.currentTarget.dataset.id)
		
	},
	closeMask: function () {
		cart.closeMask();
		this.setData({
			number: wx.getStorageSync("wholesaleNum"),
		});
	},
	chooseSpec: function (e) {
		cart.chooseSpec(e);
	
	},
	nowBuy: function () {
		cart.nowBuy();
	},
	addCart: function () {
		cart.addCart();
	},
	add: function () {
		cart.add();
	},
	less: function () {
		cart.less();
	},
    //立即购买
    buyNowTap: function (e) {
        wx.navigateTo({
            url: '../../shconfirmoder/shconfirmoder'
        })
    },
})